package IOperation;


import book.Book;
import book.Booklist;

import java.util.Scanner;

public class AddBook implements ioperation{
    @Override
    public void work(Booklist booklist) {

        //判断
        int currentNum = booklist.bookCount;
        if(currentNum == booklist.getBooks().length) {
            System.out.print("书架已满，不可存放.....");
        }
        //2.新增
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入你要增加的书名:>");
            String name = scanner.nextLine();

            String bookname = scanner.nextLine();
            System.out.print("请输入书名作者名称:>");
            String auther = scanner.nextLine();

            System.out.print("请输入书类型:>");
            String type = scanner.nextLine();

            System.out.print("请输入书的价格:>");
            double price = scanner.nextDouble();

         Book newBook = new Book(name,auther,type,price);

         //3.判断已有的书籍是否存在

        int i = 0;
        for (; i < currentNum; i++) {
            //得到下标未i书籍的引用
            Book book = booklist.getBooks(i);
            //判断
            if(book.getBookname().equals(name)){
                System.out.println("此书已有，不可增加！");
                break;
            }

            //4.增加
            if(i < currentNum){
                //将新书放入
                booklist.setBooks(currentNum,newBook);
                //书的个数+1
                booklist.setBookCount(currentNum + 1);

                System.out.println("增加成功！！！");
            }

        }





    }
}
